import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_base_app/dialog/common/tips_dialog.dart';
import 'package:flutter_base_framework/base_framework_export.dart';
import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:flutter_live_data/live_data_export.dart';
import 'package:get/get.dart';

import '../../config/pigeons/pigeons_config.dart';
import '../../constant/event_bus_key.dart';
import '../../widgets/app_bar/custom_app_bar.dart';
import '../vm/home_vm.dart';

class HomePage extends BasePage {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends BaseMvvmState<HomePage, HomeVm> with AutomaticKeepAliveClientMixin {
  _HomePageState() : super(HomeVm());
  final count = 0.obs;
  final StreamController<String> streamTest = StreamController();

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    EventBusKey.TEST.bus(this, (data) {
      LogUtils.e("首页 收到事件: $data");
    });
    title.value = "首页";
  }

  @override
  Color backgroundColor(BuildContext context) => Colors.transparent;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return callBuild(context);
  }

  @override
  Widget? floatingActionButton() {
    return FloatingActionButton(
      heroTag: hashCode,
      onPressed: () {
        vm.incrementCounter();
      },
      tooltip: 'Increment',
      child: const Icon(
        Icons.add,
        color: Colors.white,
      ),
    );
  }

  final List<String> items = List.generate(20, (index) => 'Item $index');

  @override
  Widget buildWidget(BuildContext context) {
    return GridView.builder(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3, // 每行显示的列数
        crossAxisSpacing: 10.0, // 列间距
        mainAxisSpacing: 10.0, // 行间距
      ),
      itemCount: vm.mainData.length,
      itemBuilder: (context, index) {
        return createItem(vm.mainData[index]);
      },
    );
  }

  Widget createItem(HomeItemData data) {
    return Container(
        alignment: Alignment.center,
        // color: appColors.color1,
        child: SizedBox(
          height: 40,
          child: ElevatedButton(onPressed: () async {
            if (data.type == 1) {
              "aaa".putStore("dddddd");
              // AppRouterJump.test();
              // count.value++;
              // showLoadingDialog(msg: "wwwwwww");
              // await delayFuture(3000);
              // showLoadingDialog(msg: "wwwwwww222");
              // streamTest.add("abc${count.value}");
              // AppRouterJump.login();
            } else if (data.type == 2) {
              // TipsDialog(text: "dddddd").show();
              // LogUtils.e("aaaaaaaaa ${"aaa".getStore()}");
              LogUtils.e("ddddddd ${await PigeonsConfig.api.getAppFlavor()}");
              // AppRouterJump.cmdTest();
              // () async {
              //   final result = await streamTest.stream.first;
              //   LogUtils.e("流测试结果$result");
              // }();
            }
          }, child: Obx(() {
            return Text("ddd ${vm.count}");
          })),
        ));
  }
}
